<template>
	<view>
		<view class="" style="padding: 0 20rpx;">
			<u-form :model="form" :rules="rules" ref="uForm" >
				<u-form-item  label-width="200" label="商品图片" prop="logoPath">
					<u-upload @on-remove="onRemove" @on-success="uploadSuccess" :action="sysConstant.FILE_SRC_UPLOAD" max-count="1"
						:header="uploadHeader" :file-list="uploadFileList"></u-upload>
				</u-form-item>
				<u-form-item label-width="200" label="商品分类" prop="categoryName">
					<u-input v-model="form.categoryName" @click="openSelect" :select-open="sanjiaoState" type="select"
						placehodel="请选择分类" />
					<u-select cancel-color="red" confirm-color="#2979FF" mask-close-able='false' :list="classList"
						v-model="selectShow" @confirm="selectClass" @cancel="quxiaoClass"></u-select>
				</u-form-item>

				<u-form-item label-width="200" label="商品名称" prop="goodsName">
					<u-input v-model="form.goodsName" placehodel="请输入商品名称" />
				</u-form-item>
			<!-- 	<u-form-item label-width="200" label="原价" prop="originPrice">
					<u-input v-model="form.originPrice" />
				</u-form-item> -->
				<u-form-item label-width="200" label="售价" prop="sellPrice">
					<u-input v-model="form.sellPrice" />
				</u-form-item>
				<u-form-item  label-width="200" label="会员价" prop="memberPrice">
					<u-input  v-model="form.memberPrice" />
				</u-form-item>
				<u-form-item label-width="200" label="单位" prop="unit">
					<u-input v-model="form.unit" />
				</u-form-item>
				<u-form-item label-width="200" label="排序" prop="sort">
					<u-input type="number" v-model="form.sort" />
				</u-form-item>
				<view class="" style="color: red;margin-top: 20rpx;">* 库存为 '-1' 时代表库存充足</view>
				<u-form-item label-width="200" label="库存" prop="stock">
					<u-input  v-model="form.stock" />
				</u-form-item>
				<u-form-item label-width="200" label="描述" prop="remark">
					<u-input v-model="form.remark" />
				</u-form-item>
			</u-form>
			<view class="" style="margin-top: 200rpx;">
				<u-button type='primary' @click="finsh">确定</u-button>
			</view>
			<view class="" style="width: 100%;height: 200rpx;"></view>
		</view>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	export default {
		data() {
			return {
				form: {
					logoPath: '',
					categoryName: '',
					categoryId: '',
					goodsName: '',
					originPrice: '',
					sellPrice: '',
					memberPrice: '',
					unit: '',
					sort: '',
					stock: '',
					remark: '',
				},
				selectShow: false,
				sanjiaoState: false,
				classList: [],
				sysConstant: this.sysConstant,
				uploadFileList: [],
				uploadHeader: {
					token: ''
				},
				logoPathState: false,
				rules: {
					logoPath: [{
						required: true,
						message: '请选择商品图片',
						trigger: 'blur',
					}, ],
					categoryName: [{
						required: true,
						message: '请输选择商品分类',
						trigger: 'blur',
					}, ],
					goodsName: [{
						required: true,
						message: '请输入商品名称',
						trigger: 'blur',
					}, ],
					originPrice: [{
						required: true,
						message: '请输入商品原价',
						trigger: 'blur',
						type:'number'
					}, ],
					sellPrice: [{
						required: true,
						message: '请输入商品售价且售价为数字',
						trigger: 'blur',
						type:'number'
					}, ],
					
					memberPrice: [{
						required: true,
						message: '请输入商品会员价且会员价为数字',
						trigger: 'blur',
						type:'number'
					}, ],
					unit: [{
						required: true,
						message: '请输入商品单位',
						trigger: 'blur',
					}, ],
					sort: [{
						required: true,
						message: '请输入商品排序且排序为数字',
						trigger: 'blur',
						type:'number'
					}, ],
					stock: [{
						required: true,
						message: '请输入商品库存',
						trigger: 'blur',
					}, ],
					
				},
			}
		},
		onLoad(e) {
		
			this.uploadHeader.token = getApp().globalData.token
			if(e.id){
				this.initGoodsCategory(e)
				//修改
				
			}else{
				this.initGoodsCategory(e)
			}
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
		methods: {
			uploadSuccess(e) {
				console.log("我是图片上传成功后触发的");
				console.log(e);
				this.form.logoPath = e.data.path
			},
			onRemove(){
				this.form.logoPath = ''
			},
			//获取分类列表
			initGoodsCategory(e) {
				this.$u.get("/life/goods/category/list", {
					status: 0
				}).then(res => {
					console.log("我是分类列表");
					console.log(res);
					if (res.success) {
						// this.categoryList = res.data
						for (let i = 0; i < res.data.length; i++) {
							this.classList.push({
								value: res.data[i].id,
								label: res.data[i].name
							})
						}
						console.log("我要修改了");
						console.log(e);
						if(e.id){
							this.$u.get("life/goods/info/" + e.id, {
							}).then(res => {
								console.log(res.data);
								if (res.success) {
									for(let i=0;i<this.classList.length;i++){
										console.log("666666666666666666666666");
										console.log(this.classList[i].value);
										console.log(res.data.categoryId);
										if(this.classList[i].value == res.data.categoryId){
											this.form.categoryName = this.classList[i].label
										}
									}
									this.form.logoPath = res.data.logoPath
									// this.categoryName= '',
									// for(let i=0;i<)
									this.form.categoryId = res.data.categoryId,
									this.form.goodsName = res.data.goodsName,
									this.form.originPrice = res.data.originPrice.toString(),
									this.form.sellPrice = res.data.sellPrice.toString() ,
									this.form.memberPrice =res.data.memberPrice.toString(),
									this.form.unit = res.data.unit,
									this.form.sort = res.data.sort.toString() ,
									this.form.stock = res.data.stock.toString() ,
									this.form.remark =res.data.remark.toString() ,
									this.uploadFileList.push({url:this.sysConstant.FILE_SRC_PATH + res.data.logoPath})
								}else{
									
								}
							})
						}
					}
				})
			},

			openSelect() {
				this.selectShow = true
				this.sanjiaoState = true
			},

			selectClass(e) {
				this.sanjiaoState = false
				console.log(e);
				this.form.categoryId = e[0].value
				this.form.categoryName = e[0].label
			},
			quxiaoClass(index) {
				this.sanjiaoState = false
			},
			finsh() {
				// console.log(this.form);
				// if (this.logoPathState) {

				// } else {
				// 	this.$refs.uToast.show({
				// 		title: '请上传商品图片',
				// 		type: 'warning',
				// 	})
				// }
				console.log(this.form);
				this.$refs.uForm.validate(valid => {
					if (valid) {
						this.$u.post('life/goods/save', this.form).then(res => {
							console.log(res)
							if(res.success){
								this.$refs.uToast.show({
									title: "添加成功",
									type: 'success',
								})
								setTimeout(()=>{
									uni.navigateBack({
										remark:1
									})
								},1000)
							}
						}).catch(() => {
							this.$refs.uToast.show({
								title: "请求失败",
								type: 'error',
							})
						})
						console.log(valid);
						console.log("验证通过");
					} else {
						console.log('验证失败');
					}
				});
			},
			
		}
	}
</script>

<style>

</style>